package com.woniu.car.util;

import com.woniu.car.config.ElasticSearchServerEntity;
import com.woniu.car.config.ElasticSearchShopEntity;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ElasticSearchUtil {
    @Autowired
    private ElasticsearchRestTemplate template;
    private static final String copy_to = "full_search" ;
    private static final String shopIndex = "shop";
    private static final String serverIndex = "server";
    //查询shop索引库
    public List<SearchHit<ElasticSearchShopEntity>> ElasticSearchShop(String search){
        NativeSearchQuery build = new NativeSearchQueryBuilder().withQuery(new MatchQueryBuilder(copy_to, search)).build();
        SearchHits<ElasticSearchShopEntity> search1 = template.search(build, ElasticSearchShopEntity.class);
        List<SearchHit<ElasticSearchShopEntity>> list = search1.getSearchHits();
        return list;
    }
    //更新shop索引库
    public void ElasticSearchShopUpdate(ElasticSearchShopEntity entity){
        Document document = Document.create();
        if(null!=entity.getShopName())
            document.put("shopName",entity.getShopName());
        if(null!=entity.getShopDesc())
            document.put("shopDesc",entity.getShopDesc());
        if(null!=entity.getShopAddr())
            document.put("shopAddr",entity.getShopAddr());
        if(null!=entity.getLongitudeAndShopLatitude())
            document.put("longitudeAndShopLatitude",entity.getLongitudeAndShopLatitude());
        if(null!=entity.getShopPictureUrl())
            document.put("shopPictureUrl",entity.getShopPictureUrl());
        if(null!=entity.getBusinessTime())
            document.put("businessTime",entity.getBusinessTime());
        if(null!=entity.getShopScore())
            document.put("businessTime",entity.getBusinessTime());
        UpdateResponse update = template.update(UpdateQuery.builder(entity.getId())
                        .withDocument(document)
                        .build(),
                IndexCoordinates.of(shopIndex));
    }
    //查询server索引库
    public List<SearchHit<ElasticSearchServerEntity>> ElasticSearchServer(String search){
        NativeSearchQuery build = new NativeSearchQueryBuilder().withQuery(new MatchQueryBuilder(copy_to, search)).build();
        SearchHits<ElasticSearchServerEntity> search1 = template.search(build, ElasticSearchServerEntity.class);
        List<SearchHit<ElasticSearchServerEntity>> list = search1.getSearchHits();
        return list;
    }
    
    //更新server索引库
    public void ElasticSearchServerUpdate(ElasticSearchServerEntity entity){
        Document document = Document.create();
        if(null!=entity.getServiceName())
            document.put("serviceName",entity.getServiceName());
        if(null!=entity.getDescription())
            document.put("description",entity.getDescription());
        template.update(UpdateQuery.builder(entity.getId())
                .withDocument(document)
                .build(),
                IndexCoordinates.of(serverIndex));
    }
}
